﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Pheonix.Fashionzoomer.DAL;
using Pheonix.Fashionzoomer.Entity.SqlServer2000;
using System.Collections.Generic;
using Pheonix.CodeLibrary.Data;
using System.Text;
using System.IO;
using Pheonix.CodeLibrary;

/// <summary>
/// Summary description for ProductManager
/// </summary>

namespace Pheonix.Fashionzoomer.BLL
{
    public enum SearchType
    {
        ProductSearch = 1,
        ShopSearch = 2,
        ArticleSearch = 3,
        ProductByShopSearch = 4,
        ProductByCategorySearch = 5
    }

    public sealed class ProductManager
    {
        private ProductManager() { }

        public static DataTable SearchProduct(decimal minPriceValue, decimal maxPriceValue, bool maleFemaleValue, 
            string keywordsValue)
        {
            return CustomCommand.GetProductSearchResult(minPriceValue, maxPriceValue, maleFemaleValue, keywordsValue);
        }

        public static DataTable SearchProductByShop(decimal minPriceValue, decimal maxPriceValue, 
            int brandValue, bool maleFemaleValue, string keywordsValue)
        {
            return CustomCommand.GetProductByShopSearchResult(minPriceValue, maxPriceValue, brandValue, 
                maleFemaleValue, keywordsValue);
        }

        public static DataTable SearchProductByCategory(decimal minPriceValue, decimal maxPriceValue,
            int categoryValue, bool maleFemaleValue, string keywordsValue)
        {
            return CustomCommand.GetProductByCategorySearchResult(minPriceValue, maxPriceValue,
                categoryValue, maleFemaleValue, keywordsValue);
        }

        public static DataTable GetRelatedProductList(int currentProductId, int categoryId)
        {
            return CustomCommand.GetRelatedProductTo(currentProductId, categoryId);
        }


        public static CustomCollection<Product> GetFeaturedProducts()
        {
            DataTable products = CustomCommand.GetFeaturedProductList();
            CustomCollection<Product> featuredProducts = new CustomCollection<Product>();
            foreach(DataRow row in products.Rows)
            {
                Product product = new Product();
                product.ProductCode = row["product_code"] == null ? String.Empty : row["product_code"].ToString();
                product.ImagePath = row["image_path"] == null ? String.Empty:row["image_path"].ToString();
                product.DefaultImageUrl = row["default_image_url"] == null ? String.Empty : row["default_image_url"].ToString();
                product.ProductName = row["product_name"] == null ? string.Empty : row["product_name"].ToString();
                product.Id = row["id"] == null ? Int32.MinValue : Convert.ToInt32(row["id"]);
                
                featuredProducts.Add(product);
            }
            return featuredProducts;
        }

        public static bool DeleteProductImages(List<int> idToDelete)
        {
            try
            {
                CustomCollection<Product> products = CustomCommand.GetAllProductsWithProductId(idToDelete);
                if (products != null)
                {
                    for (int i = 0; i < products.Count; i++)
                    {
                        string imageUrl = ((Product)products[i]).DefaultImageUrl;
                        string folderPath = imageUrl.Substring(0, imageUrl.LastIndexOf('/'));

                        if (Directory.Exists(HttpContext.Current.Server.MapPath(folderPath)))
                            FileAccessUtility.DeleteFolder(folderPath);
                    }
                    return true;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public static string UploadProductImage(string temporaryImageLocation, string productCode)
        {
            try
            {
                string path = null;
                string extension = Path.GetExtension(temporaryImageLocation);
                string imageBasePath = "~/upload/image/productimage/";
                string imagePath = string.Empty;

                StringBuilder imageUrl = new StringBuilder();
                imageUrl = imageUrl.Append(imageBasePath).Append(productCode).Append("/");
                imageUrl = imageUrl.Append(productCode).Append("_max_1").Append(extension);

                imagePath = HttpContext.Current.Server.MapPath(imageUrl.ToString());

                string productFolderPath = imageBasePath + productCode;
                DirectoryInfo productFolderInfo = new DirectoryInfo(productFolderPath);
                if (!productFolderInfo.Exists)
                    FileAccessUtility.CreateFolder(productFolderPath);
                else
                {
                    FileAccessUtility.DeleteFolder(productFolderPath);
                    FileAccessUtility.CreateFolder(productFolderPath);
                }

                string productThumbImage = imageUrl.ToString().Replace("max", "thumb");
                string productBigImage = imageUrl.ToString().Replace("max", "big");
                string productMidImage = imageUrl.ToString().Replace("max", "mid");

                if (ImageResizer.SaveAs(temporaryImageLocation, imageUrl.ToString(),
                 Utility.GetImageFormat(extension, true)) && ImageResizer.CreateThumbnail(imageUrl.ToString(),
                     productThumbImage, 100, false, null) && ImageResizer.CreateThumbnail(imageUrl.ToString(),
                    productMidImage, 200, false, null) && ImageResizer.CreateThumbnail(imageUrl.ToString(),
                    productBigImage, 400, false, null))
                {
                    path = productMidImage;
                    FileAccessUtility.DeleteFile(temporaryImageLocation);
                }
                else
                    path = null;

                return path;
            }
            catch
            {
                return null;
            }
        }
    }
}
